package com.excuse.excusebuilder.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.excuse.excusebuilder.R;
import com.excuse.excusebuilder.bean.Option;
import com.excuse.excusebuilder.bean.Type;

import java.util.HashMap;
import java.util.List;


/**
 * Created by U-nookia on 2017/4/16.
 */

public class HomeAdapter extends BaseExpandableListAdapter {
    private List<Type> gData;
    private List<List<Option>> iData;
    private Context mContext;
    private HashMap<Integer, Type> recordOptions;//被选中的属性
    private OnGroupAddClickListener listener;

    public OnGroupAddClickListener getListener() {
        return listener;
    }

    public void setListener(OnGroupAddClickListener listener) {
        this.listener = listener;
    }

    public HomeAdapter(List<Type> gData, List<List<Option>> iData, Context mContext) {
        this.gData = gData;
        this.iData = iData;
        this.mContext = mContext;
        this.recordOptions = new HashMap<>();
    }

    @Override
    public int getGroupCount() {
        return gData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return iData.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return gData.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return iData.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderGroup groupHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_home_group, parent, false);
            groupHolder = new ViewHolderGroup();
            groupHolder.tv_group_name = (TextView) convertView.findViewById(R.id.tv_group_name);
            groupHolder.iv_group_add = (ImageView) convertView.findViewById(R.id.iv_group_add);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (ViewHolderGroup) convertView.getTag();
        }

        groupHolder.tv_group_name.setText(gData.get(groupPosition).getName());
        groupHolder.iv_group_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick((Type) getGroup(groupPosition));
                }
            }
        });
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolderItem itemHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_home_child, parent, false);
            itemHolder = new ViewHolderItem();
            itemHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            convertView.setTag(itemHolder);
        } else {
            itemHolder = (ViewHolderItem) convertView.getTag();
        }
        itemHolder.tv_name.setText(iData.get(groupPosition).get(childPosition).getName());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private static class ViewHolderGroup {
        private TextView tv_group_name;
        private ImageView iv_group_add;
    }

    public interface OnGroupAddClickListener {
        void onClick(Type type);
    }

    private static class ViewHolderItem {
        private TextView tv_name;
    }

}
